<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      .score {
        border: 1px solid #ccc;
        border-collapse: collapse;
      }

      .score th {
        background-color: #f4f4f4;
      }

      .score th,
      .score td {
        width: 120px;
        text-align: center;
        padding: 5px;
        border: 1px solid #ccc;
        font-size: 13px;
      }

      .score tr:last-of-type td div {
        display: flex;
        justify-content: space-around;
      }
      input {
        outline: none;
      }
      td.active {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <table class="score">
        <thead>
          <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody v-if="scoreList.length">
          <tr v-for="(item, index) in scoreList" :key="item.id">
            <td>{{ index + 1 }}</td>
            <td>{{ item.ke_mu }}</td>
            <td :class="{active: item.score < 60}">{{ item.score }}</td>
            <td>
              <a href="#" @click.prevent="del(item.id)">删除</a>
            </td>
          </tr>
          <tr>
            <td colspan="4">
              <div>
                <span>总分: {{ zon }}</span>
                <span>平均分: {{ avg }}</span>
              </div>
            </td>
          </tr>
        </tbody>
        <tbody v-else>
          <tr>
            <td colspan="4">暂无数据</td>
          </tr>
        </tbody>
      </table>
      <form>
        科目：<input
          type="text"
          placeholder="请输入科目"
          v-model.trim="ke_mu"
        /><br />
        分数：<input
          type="text"
          placeholder="请输入分数"
          v-model.number="score"
        /><br />
        <button @click.prevent="add">添加</button>
      </form>
    </div>
    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#box",
        data: {
          scoreList: [
            { id: 0, ke_mu: "语文", score: 60 },
            { id: 1, ke_mu: "英语", score: 47 },
            { id: 2, ke_mu: "数学", score: 76 },
          ], // 成绩列表
          ke_mu: "",
          score: "",
        },
        methods: {
          del(id) {
            this.scoreList = this.scoreList.filter((item) => {
              return item.id !== id;
            });
          },
          add() {
            if (this.ke_mu !== "" && this.score !== "") {
              this.scoreList.push({
                id: +new Date(),
                ke_mu: this.ke_mu,
                score: this.score,
              });
              this.ke_mu = "";
              this.score = "";
            }
          },
        },
        computed: {
          zon() {
            return this.scoreList.reduce((zon, item) => {
              return zon + item.score;
            }, 0);
          },
          avg() {
            return (this.zon / this.scoreList.length).toFixed(2);
          },
        },
      });
    </script>
  </body>
</html>
